﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Npgsql;
using System.Data;

namespace SIGEB.Modelo.moduloDocumentos
{
    public class conjuntoReservas:conjuntoBase
    {
        #region "constructores"

        public conjuntoReservas(datosAplicacion dA)
            : base(dA)
        {
            this.numTipo = conjuntoBase.CONJRESERVAS;
        }

        #endregion

        #region "metodos"

        public override object obtenerEstado()
        {
            throw new NotImplementedException();
        }

        #region "imec"

        public void agregarReserva(reserva nuevaRes)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT registrar_solicitud('" + formatDate(nuevaRes.solicitud) + "', '" +
                    formatDate(nuevaRes.vencimiento) + "', '" + nuevaRes.idUsuario + "', '" + nuevaRes.idDocumento + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch(Exception e)
            {
                throw new Exception(e.Message + "Error al agregar reserva");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            notificar(this.numTipo, null);
        }

        public void eliminarReserva(String idReserva)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT eliminar_solicitud('" + idReserva + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                ds.Reset();
                da.Fill(ds);
                dt = ds.Tables[0];
            }
            catch
            {
                throw new Exception("Error al eliminar reserva");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            notificar(this.numTipo, null);
        }

        public void eliminarReserva(String idUsuario, String idDocumento)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT eliminar_solicitud_usuario_documento('" + idUsuario + "', '" + idDocumento + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                ds.Reset();
                da.Fill(ds);
                dt = ds.Tables[0];
            }
            catch
            {
                throw new Exception("Error al eliminar reserva");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            notificar(this.numTipo, null);
        }

        public reserva obtenerreserva(String codreserva)
        {
            reserva nueva=null;
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_solicitud_id('" + codreserva + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 5);
                    nueva = new reserva(valores[0], formatDate(valores[1]), formatDate(valores[2]), valores[3], valores[4]);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el reserva " + codreserva);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            return nueva;
        }

        public LinkedList<reserva> obtenerreservaUsuario(String idUsuario)
        {
            LinkedList<reserva> nueva = new LinkedList<reserva>();
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_solicitud_idUsuario('" + idUsuario + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 5);
                    nueva.AddLast(new reserva(valores[0], formatDate(valores[1]), formatDate(valores[2]), valores[3], valores[4]));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el reserva " + idUsuario);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            return nueva;
        }

        public LinkedList<reserva> obtenerReservas()
        {
            LinkedList<reserva> nueva = new LinkedList<reserva>();
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_solicitud()";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 5);
                    nueva.AddLast(new reserva(valores[0], formatDate(valores[1]), formatDate(valores[2]), valores[3], valores[4]));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el reservas");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            return nueva;
        }

        #endregion

        #endregion
    }
}
